package hrms.controller;

import hrms.entity.Admin;
import hrms.entity.Company;
import hrms.entity.History;
import hrms.service.AdminService;
import hrms.service.CompanyService;
import hrms.service.HistoryServiceImpl;
import hrms.util.LogUtil;
import hrms.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system")
public class SystemController
{
	@Resource(name = "companyService")
	private CompanyService companyService;

	@Resource(name = "adminService")
	private AdminService adminService;

	@Resource(name = "historyService")
	private HistoryServiceImpl historyService;

	private String adminCode;

	@ExceptionHandler
	public String exHandle(Exception ex, HttpServletRequest req)
	{
		LogUtil.saveEx(ex);
		return "error";
	}

	@ResponseBody
	@RequestMapping("/loadSystem.sls")
	public Result<List<Company>> executeA(HttpServletRequest req, HttpServletResponse res)
	{
		String page = req.getParameter("page");
		Result<List<Company>> result = companyService.findByPage(page, res);
		return result;
	}

	@ResponseBody
	@RequestMapping("/loadAdmin.sls")
	public Result<List<Admin>> executeA1(HttpServletRequest req, HttpServletResponse res)
	{
		String page = req.getParameter("page");
		Result<List<Admin>> result = adminService.findByPage(page, res);
		return result;
	}

	@ResponseBody
	@RequestMapping("/loadHistory.sls")
	public Result<List<History>> executeA2(HttpServletRequest req, HttpServletResponse res)
	{
		String page = req.getParameter("page");
		Result<List<History>> result = historyService.findByPage(page, res);
		return result;
	}

	@ResponseBody
	@RequestMapping("/page.sls")
	public Result<Map<String, Integer>> executeB(HttpServletRequest req)
	{
		Result<Map<String, Integer>> result = companyService.pageTotal(req);
		return result;
	}

	@ResponseBody
	@RequestMapping("/adminPage.sls")
	public Result<Map<String, Integer>> executeB1(HttpServletRequest req)
	{
		Result<Map<String, Integer>> result = adminService.pageTotal(req);
		return result;
	}

	@ResponseBody
	@RequestMapping("/historyPage.sls")
	public Result<Map<String, Integer>> executeB2(HttpServletRequest req)
	{
		Result<Map<String, Integer>> result = historyService.pageTotal(req);
		return result;
	}

	@ResponseBody
	@RequestMapping("/deleteCompanyInfo.sls")
	public Result<Object> executeC(String id, HttpServletRequest req)
	{
		return companyService.deleteCompanyInfo(id, req);
	}

	@ResponseBody
	@RequestMapping("/companyInfo.sls")
	public Result<Company> executeD(String id)
	{
		return companyService.findOne(id);
	}

	@ResponseBody
	@RequestMapping("/updateCompanyInfo.sls")
	public Result<Object> executeE(HttpServletRequest req)
	{
		return companyService.updateCompanyInfo(req);
	}

	@ResponseBody
	@RequestMapping("/addCompanyInfo.sls")
	public Result<Object> executeF(HttpServletRequest req)
	{
		return companyService.addCompanyInfo(req);
	}

	@ResponseBody
	@RequestMapping("/changePwd.sls")
	public Result<Object> executeG(HttpServletRequest req)
	{
		String oldPwd = req.getParameter("old");
		String newPwd = req.getParameter("new");
		Admin admin = (Admin) req.getSession().getAttribute("admin");
		if (admin == null)
		{
			Result<Object> result = new Result<Object>();
			result.setStatus(3);
			result.setMsg("登录超时!");
			return result;
		}
		else
		{
			Integer adminId = admin.getAdminId();
			return adminService.changePwd(oldPwd, newPwd, adminId);
		}
	}
	
	@ResponseBody
	@RequestMapping("/createAdmin.sls")
	public Result<Object> executeH(HttpServletRequest req)
	{
		Integer empId = Integer.parseInt(req.getParameter("empId"));
		String empName = req.getParameter("empName");
		String adminCode = req.getParameter("name");
		String pwd = req.getParameter("pwd");
		Integer roll = Integer.parseInt(req.getParameter("roll"));
		Admin admin = (Admin) req.getSession().getAttribute("admin");
		if (admin == null)
		{
			Result<Object> result = new Result<Object>();
			result.setStatus(5);
			result.setMsg("登录超时!");
			return result;
		}
		else
		{
			Integer userRoll = admin.getRoll();
			if (roll == 1 && userRoll != 0)
			{
				Result<Object> result = new Result<Object>();
				result.setStatus(6);
				result.setMsg("权限不够!");
				return result;
			}
			return adminService.addAdmin(empId, adminCode, empName, pwd, roll);
		}
	}

	@ResponseBody
	@RequestMapping("/deleteAdmin.sls")
	public Result<Object> executeI(HttpServletRequest req)
	{
		String adminId = req.getParameter("id");
		return adminService.deleteAdmin(adminId, req);
	}

}
